<template>
  <div class="uploadImg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img v-if="imageUrl" v-imgError="defaultImg" :src="imageUrl" class="avatar" @click.stop="showBigImage">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <div v-if="imageUrl" class="close" @click="closeEvent">X</div>
    <!-- 图片预览 -->
    <el-dialog :visible.sync="isShow" title="图片预览">
      <div style="text-align: center">
        <img class="img" :src="imageUrl">
      </div>
    </el-dialog>
    <!-- <el-progress :percentage="70"></el-progress> -->
    <!-- 进度条 -->
    <transition name="xxx">
      <el-progress v-if="showProgress" :percentage="percentage" />
    </transition>
  </div>
</template>
<script>
// 安装,并导入腾讯云插件
import Cos from 'cos-js-sdk-v5'
// 配置密钥
const cos = new Cos({
  SecretId: 'AKID79uPdaHhpgIdhU1HGzQb6OREk00qRp6C', // 身份识别 ID
  SecretKey: 'rTosgDSQ9fwykIyPZS4PUIq78rKXT9Zj' // 身份密钥
})
export default {
  name: 'UploadImg',
  model: {
    prop: 'imageUrl'
  },
  props: {
    imageUrl: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // imageUrl: ''
      percentage: 0,
      showProgress: false,
      isShow: false,
      defaultImg: require('@/assets/common/head.jpg')

    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      // 上传前,限制文件大小与类型
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    httpRequest(res) {
      console.log(res, 333)
      this.showProgress = true
      this.percentage = 0
      cos.putObject({
        Bucket: 'wxxx-1308460234', /* 存储桶: */
        Region: 'ap-beijing', /* 存储桶所在地域，必须字段 */
        Key: res.file.uid + '', /* 必须是字符串 */
        StorageClass: 'STANDARD', // 固定值
        Body: res.file, // 上传文件对象
        onProgress: (progressData) => {
          // 上传进度
          this.percentage = progressData.percent * 100
          if (progressData.percent === 1) {
            setTimeout(() => {
              this.showProgress = false
            }, 1000)
          }
          console.log(JSON.stringify(progressData))
        }
      }, (err, data) => {
        // 上传成功或者失败的回调
        console.log(err || data)
        this.$emit('input', 'http://' + data.Location)
        // this.imageUrl = 'http://' + data.Location
      })
    },
    closeEvent() {
      this.$emit('input', '')
    },
    showBigImage() {
      // 图片点击处理
      this.isShow = true
    }
  }
}
</script>

<style lang="scss" csoped>
 ::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
 ::v-deep .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .uploadImg{
    position: relative;
    width: 178px;
    &:hover{
      .close{
        display: block;
      }
    }
    .close{
      display: none;
      position: absolute;
      top: 5px;
      right: 10px;
      cursor: pointer;
    }
    .img{
      width: 100%;
    }
  }
  .xxx-enter-active,
  .xxx-leave-active {
    transition: all 0.5s;
  }
  .xxx-enter,
  .xxx-leave-to {
    opacity: 0;
  }
</style>
